using System;
using System.Configuration;
using System.Windows.Forms;
using Pegasus.Windows.Forms;
using RollManager.Forms.ActionPanels;
using RollManager.Forms.MainPanels;
using RollManager.Library;

namespace RollManager.Forms
{
    public partial class MainForm : FormEx
    {
        public MainForm() : base( Common.GetConfigName( null ) )
        {
            // Give this app the auth rights of the user.
            AppDomain.CurrentDomain.SetPrincipalPolicy( System.Security.Principal.PrincipalPolicy.WindowsPrincipal );

            Database.Init( ConfigurationManager.AppSettings[ "DBServer" ], ConfigurationManager.AppSettings[ "DBName" ] );

            InitializeComponent();
            actionsPanel1.Refresh();
        }

        private void tabControl1_SelectedIndexChanged( object sender, EventArgs e )
        {
            tabControl1.TabPages[ tabControl1.SelectedIndex ].Controls[ 0 ].Refresh();
        }

        private void MainForm_FormClosing( object sender, FormClosingEventArgs e )
        {
            saveTabPage( tabControl1.SelectedIndex );
            ActionDialog.SaveWindowPosition();
        }

        private void tabControl1_Deselecting( object sender, TabControlCancelEventArgs e )
        {
            saveTabPage( e.TabPageIndex );
        }

        private void saveTabPage( int tabIndex )
        {
            ( (MainPanel) tabControl1.TabPages[ tabIndex ].Controls[ 0 ] ).Save();
        }

        private void MainForm_Load( object sender, EventArgs e )
        {
            // Ensure user exists in database by asking for them.
            Database.GetUserId( Environment.UserName );
        }
    }
}